Sidebar To Custom Post Type

Hi there,

i´m trying to assign a sidebar to my custom post type “depoimentos”

i changed the functions.php:
"
function add_sidebar_custom_post() {
if( get_post_type() == ‘depoimentos’) {
return ‘ups-sidebar-depoimentos-sidebar’;
}
}
add_filter( ‘ups_sidebar’, ‘add_sidebar_custom_post’ );
"
and the wp-single-depoimentos.php:

"

<?php // ============================================================================= // VIEWS/RENEW/WP-SINGLE-depoimentos.PHP // ----------------------------------------------------------------------------- // Single post output for Renew. // ============================================================================= $fullwidth = get_post_meta( get_the_ID(), '_x_post_layout', true ); ?> <?php get_header(); ?> <?php global $post; ?>

  <?php while ( have_posts() ) : the_post(); ?>
    <?php x_get_view( 'renew', 'content-depoimentos', get_post_format() ); ?>

<?php the_field('depoimento'); ?>

    <?php x_get_view( 'global', '_comments-template' ); ?>
  <?php endwhile; ?>

</div>

<?php if ( $fullwidth != 'on' ) : ?>
  <?php get_sidebar(); ?>
<?php endif; ?>
<?php get_footer(); ?>

"

What is missing?

Hi There,

Is your setup here set to fullwidth or with sidebar?

If your settings for that is either Content Left, Sidebar Right or Sidebar Left, Content Right, the first code you have shared should be enough:

function add_sidebar_custom_post() {
if( get_post_type() == 'depoimentos') {
return 'ups-sidebar-depoimentos-sidebar';
}
}
add_filter( 'ups_sidebar', 'add_sidebar_custom_post' );

I am not sure about your setup so please share your site URL and URL of your custom post type so we can check where you at.

Hi! thanks for the answer,

the sidebar did appear but below the content (see JPG attached)… but how can i set it to be next to the text?

… and how to show, on the sidebar, the recent posts from a specific Custom post Type.

Thanks

Hi There,

Your site is under construction. We need credentials to see the page. Please do share wp credentials and FTP credentials to on a secure note so we can check further. Thank you.

Hi there,

here it goes…

tks

Hi There,

It is because you have got the class wrong. lef should be left. See this: https://screencast-o-matic.com/watch/cb6nXaI4qV
Look for your custom template and correct the class.

:slight_smile: Thank you…

now, how to show, on this sidebar, the recent posts from that specific Custom post Type?

Thanks again

Hi,

You can try this third party plugin.

Hope that helps.

Thanks, that was what i wanted. :slight_smile:

Hi,

now for the other page (other CPT) i want to add this recent posts not as a sidebar but below the post, as the “recent post” from the cornerstone (see attached), how can i do this?

When i click in one of these CPT i want to see the same recent post layout.

My problem is that the displayed page was made from cornerstone and the other page, with it below, is a CPT and need to be made through php.

Thank you!

Hello There,

Thanks for updating in! You can make use of the before the content hook so that the recent post shortcode can be added at the bottom of your CPT article. You can take this code and an example:


function add_recent_cpt(){
  if ( is_singular( 'your_cpt' ) ) {
    echo do_shortcode('[x_recent_posts type="your_cpt" count="4" orientation="horizontal"]')
  }
}
add_action('x_before_the_content_end', 'add_recent_cpt');

This code serves as an example. Please do not forget to change the correct custom post type in the code before adding it to your child theme’s functions.php file.

Hope this helps. Please let us know how it goes.

Hi there,
i tried but it returned this:

“Parse error: syntax error, unexpected ‘}’, expecting ‘,’ or ‘;’ in /var/www/vhosts/dsc-curitiba.com/httpdocs/wp-content/themes/x-child/functions.php on line 52”

what’s wrong?

Thanks

Hi there,

Ah, please add semi-colon at the end of a line, like this

echo do_shortcode('[x_recent_posts type="your_cpt" count="4" orientation="horizontal"]');

Thanks!

Hi there,

thanks now there is no error but nothing happens also…

do i have to add anything in another file (wp-single-my-cpt.php e.g.) to make this x_recent_posts appear on the page?

thanks again

Hi there,

Could you try this code instead? Maybe it’s picking up the legacy templates. Usually adding extra content under the main is done through the_content hook.

function add_recent_cpt( $content ){
  if ( is_singular( 'exalunos' ) ) {
    return $content.do_shortcode('[x_recent_posts type="exalunos" count="3" orientation="horizontal"]');
  }

return $content;
}
 add_filter('the_content', 'add_recent_cpt', 9999999999999);

Thanks!

Hi there,

thanks but still not working…

any other idea?

Thks

Hi There,

Please check this thread too:


This custom code you have will only add custom post type on Cornerstone recent post element and not on the recent post shortcode:

// adicionar CPT "depoimentos" e "exalnos" ao recent posts do cornerstone
function add_event_post_type( $types ) {
  $types['depoimentos'] = 'depoimentos';
  $types['exalunos'] = 'exalunos';
  $types['novidades'] = 'novidades';
  return $types;

}
add_filter( 'cs_recent_posts_post_types', 'add_event_post_type', 999 );

It is not working because those custom post type is not yet added by default on the shortcode. Try using default post and the shortcode will display content. Now check the two links as guide on how to add custom post type on the recent post shortcode. The original code can be found here: wp-content\plugin\cornerstone\includes\shortcodes\recent-posts.php

Hope this helps.

Hi there,
the links you sent are not working anymore … can you update them?

tks

Hi @apxds,

I just checked the above links in different browsers and they’re working fine now. Can you please try the links now?

Thanks!