Sidebar is appearing on custom post pages without being called - php

I'm creating a Wordpress theme for a graphic portfolio website, and I'm utilizing the sidebar.php for a hero image on the index page that is not meant to appear on other pages. While this works for all static inside pages, I have two custom post categories, and the sidebar section has started appearing above those posts.
I should note that this hasn't always been a problem, and started cropping up for no apparent reason while working on something else.
On index.php, the sidebar is called as follows:
<?php get_header(); ?>
<?php get_sidebar(); ?>
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
endwhile; ?>
<nav>
<ul class="pager">
<li><?php next_posts_link( 'Previous' ); ?></li>
<li><?php previous_posts_link( 'Next' ); ?></li>
</ul>
</nav>
<?php endif;?>
<?php get_footer(); ?>
While page.php is similar but lacks the php line calling the sidebar, as follows:
<?php get_header(); ?>
<div class="row">
<div class="col-sm-12">
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
endwhile; endif;?>
</div> <!-- /.col -->
</div> <!-- /.row -->
<?php get_footer(); ?>
page-fine-art.php also lacks that php line, as follows:
<div class="row">
<div class="col-sm-12">
<?php
$args = array(
'post_type' => 'fine-art',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$custom_query = new WP_Query( $args );
while ($custom_query->have_posts()) : $custom_query->the_post(); ?>
<div class="blog-post">
<h2 class="blog-post-title"><? php the_title(); ?></h2>
<p class="blog-post-meta"><?php the_date(); ?> by <?php the_author(); ?></p>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} ?>
<?php the_excerpt(); ?>
<?php endwhile; ?>
</div> <!-- /.col -->
</div> <!-- /.row -->
<?php get_footer(); ?>
The function creating the the custom posts for "fine art" goes:
function create_my_custom_posts() {
register_post_type( 'fine-art',
array(
'labels' => array(
'name' => __( 'Fine Art' ),
'singular_name' => __( 'Fine Art' ),
),
'public' => true,
'has_archive' => true,
'supports' => array(
'title',
'editor',
'thumbnail',
'custom-fields'
)
));
I don't see where sidebar.php is being called in the final example, or where else it could be coming from.

Related

Wordpress page content and posts in the same page

I'm pretty new in world of wordpress and I'm trying to adjust HTML page into wordpress theme.
I need a page content to be displayed first on a page and under that, the posts should be shown. But what I'm getting are just posts shown twice on the page (where page content should be). Is there any possibility to overcome this?
And additional question, how to filter posts according to their category? I've tried with query_posts('cat=Small'), but it doesn't seem to work properly.
The code for index.php looks as following:
<?php get_header(); ?>
<?php
wp_reset_query();
while ( have_posts() ) : the_post();
the_content();
endwhile;
wp_reset_query();
?>
<section>
<header class="major">
<h2>Erat lacinia</h2>
</header>
<div class="features">
<?php query_posts('cat=Small'); ?>
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<article>
<span class="icon fa-diamond"></span>
<div class="content">
<h3><?php the_title(); ?></h3>
<p><?php the_content('Read More'); ?></p>
</div>
</article>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
<?php get_footer(); ?>
Try the below code. This may help you
<section>
<div class="major">
<h2>Erat lacinia</h2>
</div>
<div class="features">
<?php $args = array(
'posts_per_page' => -1,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'ASC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => true
);?>
<?php query_posts( $args ); ?>
<?php while ( have_posts() ) : the_post(); ?>
<article>
<span class="icon fa-diamond"></span>
<div class="content">
<h3><?php the_title(); ?></h3>
<p><?php the_content('Read More'); ?></p>
</div>
</article>
<?php endwhile; wp_reset_query(); ?>
</div>
</section>
You can use two loops.
In your php page template, first execute the regular loop to get the content of the actual page, like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
//output page content here
<?php endif; ?>
Then you define a new query for the desired posts:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'ASC',
)
);
//(Add and change arguments as desired in the array above)
$loop1 = new WP_Query($args);
if ( $loop1->have_posts() ) : while ( $loop1->have_posts() ) : $loop1->the_post();
//Output the post contents in a loop here
<?php endif;
wp_reset_postdata();?>
And then add the rest of the page template (footer etc.)
<?php
/*
*Template name: test
*/
get_header();
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$attrs = array(
'numberposts' => 10,
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'small' )
)
)
);
$my_posts = get_posts( $attrs );
the_content();
?>
<?php if ($my_posts): ?>
<section>
<header class="major">
<h2>Erat lacinia</h2>
</header>
<div class="features">
<?php foreach ($my_posts as $key => $value): ?>
<article>
<span class="icon fa-diamond"></span>
<div class="content">
<h3><?= $value->post_title; ?></h3>
<p><?= $value->post_content ?></p>
</div>
</article>
<?php endforeach ?>
</div>
</section>
<?php endif ?>
<?php
endwhile;
else :
echo wpautop( 'Sorry, no posts were found' );
endif;
get_footer(); ?>

how to customize post in wordpress?

I try to customize my own post using the shortcode way in wordpress. I want to display the post according to its category.
<?php
function sample_post($atts) {
extract(shortcode_atts(array(
'category_name' => 'uncategorized'
), $atts));
$args = array('category_name' => $category_name);
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post(); ?>
<label><?php the_title(); ?></label>
<?php
endwhile;
endif;
}
add_shortcode('sample_post', 'sample_post');
?>
the code is fine and I have a file in my template name content-page.php
here is the code of my content-page
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<?php the_content(); ?>
<?php
wp_link_pages( array(
'before' => '<div class="page-links">' . __( 'Pages:', 'unite' ),
'after' => '</div>',
) );
edit_post_link( __( 'Edit', 'foodgrower' ), '<span class="edit-link">', '</span>' );
?>
</div><!-- .entry-content -->
</article><!-- #post-## -->
I don't know if I miss something about the setup or query on it. Here's I really want to displays. I only want to display <label><?php the_title(); ?></label> (the title of the post), but now it displays the content-page.php. I don't get it also why it appears in my page. I didn't call the content-page.php to display in my page.
Change your code to return the data, like the following,
function sample_post($atts) {
$return = '';
extract(shortcode_atts(array(
'category_name' => 'uncategorized'
), $atts));
$args = array('category_name' => $category_name);
$the_query = new WP_Query( $args );
if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post();
$return .= "<label>".the_title()."</label>";
endwhile;
endif;
return $return;
}
add_shortcode('sample_post', 'sample_post');

Getting excerpt in loop within loop – Twenty sixteen theme Wordpress

Hello fellow wordpress expirienced users.
I’m having a struggle while doing a latest 3 posts excerpt with thumbnail in my homepage in Wordpress TwentySixteen Theme.
I tried various possibilities but i cant get it running.
The goal is to look the posts like this:
http://caenthemes.cekuj.net/?s=p%C5%99%C3%ADsp%C4%9Bvek
My thought is to use an already made template for the seach page.
But than it the text of the excerpt is nowhere:
http://caenthemes.cekuj.net/
The fact that its not styled leave aside please.
The code of the main page:
<?php
/**
* The template for displaying main-page without title.
* #package WordPress
* #subpackage Twenty_Sixteen
* #since Twenty Sixteen 1.0
*/
?>
<section id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<!--<header class="entry-header">
<?php the_title('<h1 class="entry-title">', '</h1>'); ?>
</header><!-- .entry-header -->
<div class="wp-page-content">
<?php
the_content();
wp_link_pages(array(
'before' => '<div class="page-links"><span class="page-links-title">' . __('Pages:', 'twentysixteen') . '</span>',
'after' => '</div>',
'link_before' => '<span>',
'link_after' => '</span>',
'pagelink' => '<span class="screen-reader-text">' . __('Page', 'twentysixteen') . ' </span>%',
'separator' => '<span class="screen-reader-text">, </span>',
));
?>
</div><!-- .entry-content -->
</section>
<section>
<header class="entry-header">
<h2>
<?php
if (get_locale() == 'cs_CZ') {
echo "Nejnovější příspěvky";
} else {
echo "Latest posts";
}
?>
</h2>
</header><!-- .entry-header -->
<?php
$args = array(
'posts_per_page' => 3,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'author_name' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$myposts = get_posts($args);
foreach ($myposts as $post) : setup_postdata($post);
get_template_part('template-parts/content', 'search');
endforeach;
wp_reset_postdata();
?>
</section><!-- #wp-page-content-## -->
I’m 80% sure that i’m not handling right the inner loop within the main loop of twenty sixteen. Just to cover all posibilities i was also trying to do it not via get template but still i only get the posts categories titles and thumbnails but not the excerpt.
Can you help me with this ?
The whole page is based on twenty sixteen theme with my modifications.
Thank you very much,
Caen Ragestorm
For getting post details such as title,content and featured image you can use this code :
$latestPost = new WP_Query( array( 'post_type' => 'posts', 'posts_per_page' =>-1,'order' => 'ASC') );
while ( $latestPost->have_posts() ) : $latestPost->the_post();
$sTitle = the_title();
$sContent = the_content();
$feat_image_latestPost = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
endwhile;
Re write CSS according to your requirement.
Thanks to SJP i made it work. So the full code working for me is here, hope anyone else will find it usefull:
<section id="latest-posts">
<header class="entry-header">
<h2>
<?php
if (get_locale() == 'cs_CZ') {
echo "Nejnovější příspěvky";
} else {
echo "Latest posts";
}
?>
</h2>
</header><!-- .entry-header -->
<div class="entry-content">
<?php
$args = array(
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$latestPost = new WP_Query($args);
while ($latestPost->have_posts()) : $latestPost->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php the_title(sprintf('<h2 class="entry-title">', esc_url(get_permalink())), '</h2>'); ?>
</header><!-- .entry-header -->
<div class="post-summary">
<a class="post-thumbnail" href="<?php the_permalink(); ?>" aria-hidden="true">
<?php the_post_thumbnail('post-thumbnail', array('alt' => the_title_attribute('echo=0'))); ?>
</a>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div>
</div>
<?php if ('post' === get_post_type()) : ?>
<footer class="entry-footer">
<?php twentysixteen_entry_meta(); ?>
<?php
edit_post_link(
sprintf(
/* translators: %s: Name of current post */
__('Edit<span class="screen-reader-text"> "%s"</span>', 'twentysixteen'), get_the_title()
), '<span class="edit-link">', '</span>'
);
?>
</footer><!-- .entry-footer -->
<?php else : ?>
<?php
edit_post_link(
sprintf(
/* translators: %s: Name of current post */
__('Edit<span class="screen-reader-text"> "%s"</span>', 'twentysixteen'), get_the_title()
), '<footer class="entry-footer"><span class="edit-link">', '</span></footer><!-- .entry-footer -->'
);
?>
<?php endif; ?>
</article>
<?php
endwhile;
wp_reset_postdata();
?>
</div>
</section>
Nice day to you all.
Caen Ragestorm
www.CaenRagestorm.cz

WordPress: Adding a widget area to the primary page template?

I'm trying to add a widget area to the main body of page.php, so that I can place unique widgets on each page of my site. I've followed the instructions here: http://codex.wordpress.org/Widgetizing_Themes
I added this to functions.php in my theme:
function arphabet_widgets_init() {
register_sidebar( array(
'name' => 'Main widget',
'id' => 'main_widget_area',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2 class="rounded">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'arphabet_widgets_init' );
and this is page.php:
get_header(); ?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php if ( is_active_sidebar( 'main_widget_area' ) ) : ?>
<div id="main-widget" class="widget-area main-widget">
<?php dynamic_sidebar( 'main_widget_area' ); ?>
</div><!-- #primary-sidebar -->
<?php endif; ?>
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>
<div class="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentythirteen' ) . '</span>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
</div><!-- .entry-content -->
<footer class="entry-meta">
<?php edit_post_link( __( 'Edit', 'twentythirteen' ), '<span class="edit-link">', '</span>' ); ?>
</footer><!-- .entry-meta -->
</article><!-- #post -->
<?php comments_template(); ?>
<?php endwhile; ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
(I added the top section, with main_widget_area)
However when I edit the page I see no way to drop in a widget. I'm not sure if I'm missing a step, or if I'm just not editing the page in the right way.
Take a look at the "Widgets" menu underneath "Appearance" in the WordPress backend. There will be a list of widgets on the left side of the page and a list of available sidebars on the right. Just drag-and-drop the widget you want into the sidebar you registered and you should be good to go.

get_posts on a WP static page

This is my first time asking a question on this forum. I hope i can be specific enough. I am trying to replace the front page posts section of a WP template. I have the code below running fine on my index.php page (it gets the posts) when the WP theme is set to settings>reading>your latest posts BUT the way the WP theme "the thinker" is setup reading is set to static page and the posts are gotten through a blog template page (in same location as the index.php file) which gets the posts from loops in separately included files. I'd like to keep it set to static for a few reasons. My question is "Is there a reason why the code below would only work in an index.php file and not out of a blog-template file which is is in the same location as the index.php file. I've checked and the the template-parts called in the code are being called. It seems like there's just no posts to get (which there are).
Thank you for your time,
Dave
<!-- blog content -->
<div class="container">
<div class="row" id="primary">
<main id="content" class="col-sm-8" role="main">
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
get_template_part( 'template-parts/content', get_post_format() );
?>
<?php endwhile; ?>
<?php the_posts_navigation(); ?>
<?php else : ?>
<?php get_template_part( 'template-parts/content', 'none' ); ?>
<?php endif; ?>
</main><!-- content -->
<!-- sidebar -->
<aside class="col-sm-4">
<?php get_sidebar(); ?>
</aside>
</div><!-- primary -->
</div><!-- container -->
have_posts() functions only allowed you to check whether the page have the post or not and as it is your static pages, there will be no posts assign to this page.
You need to query first at the start of the page to show posts. Here is the example.
EDITED::
$args = array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'movie_genre',
'field' => 'slug',
'terms' => array( 'action', 'comedy' ),
),
array(
'taxonomy' => 'actor',
'field' => 'term_id',
'terms' => array( 103, 115, 206 ),
'operator' => 'NOT IN',
),
),
);
$query = new WP_Query( $args );
if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
You can find more about WP_Query in below link.

Categories