One post in two archives - php

Well, this one is getting me quite frustrated. I have multiple custom post types (CPT). Most of them have an archive. Now I have a ctp called Portfolio and a cpt called recent work.
What I would like is to have both ctp's show up in the archives-[slug].php pages. BUT in one archive the link should be /portfolio/page-name and in the other it should be /recent-work/page-name.
Both pages would show the same content only the archive slug would be different. I don't want to have to create double content.
Does anyone know if this is possible using Wordpress and how I can achieve this...
As Gavin Thomas said, use categories (well in my case custom taxonomies). This is my loop code:
<?php $args = array(
'post_type' => 'portfolio',
'posts_per_page'=> -1,
'orderby' => 'ID',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'event-type',
'field' => 'slug',
'terms' => 'corporate-events'
)
)
);
$products = new WP_Query( $args );
if( $products->have_posts() ) {
while( $products->have_posts() ) {
$products->the_post();
?>
<?php $naam = get_field('naambedrijf');
$soort = get_field('soort_uitje');
$foto = get_field('flickr_fotoset'); ?>
<div class="col s6 m4 l4">
<a href="<?php the_permalink(); ?>">
<div class="title">
<h2 class="truncate" style="line-height:20px;"><?php echo $naam; ?></h2>
<small class="truncate" style="color:#222;"><?php echo $soort; ?></small>
</div>
<div class="thumb">
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} ;?>
</div>
</a>
</div>
<?php
}
}
else {
echo 'There seems to be a problem, please try searching again or contact customer support!';
} ?>
The items under the ctp are displayed using this code, but the permalink is still /portfolio/page-name and not /category-slug/page-name. How do I go about this?

Related

Wordpress Understrap display posts on a page

I'm learning how to use wordpress API. I'm a newbie with this framework, so I've decided to install Understrap to use the Bootstrap 4 framework and create a simple portfolio website. After googling a bit, I've started experimenting with the code, but there are many aspects of this wordpress theme that are unclear to me. I want to display some posts on a page and style how they will appear using the bootstrap classes markup. Is there any valid tutorial about or anyone can suggest to me the correct modifications I need to make to the template theme files?
I've tried to create a page named postpage.php with this code inside, but it will not be recognized from wordpress as a template model for a page.
CODE:
<?php
$args = array(
'posts_per_page' => 6,
'offset' => 0,
'category' => 'portfolio',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '', 'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post', 'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php
endforeach;
wp_reset_postdata();
?>
First you need to specify that this is a page template by adding the following code to the top of your file:
<?php /* Template Name: Example Template */ ?>
Then it will show up in your page template dropdown. More info about page templates here.
In order to add Boostrap classes, you need to wrap the foreach statement in the Bootstrap containers and then change the ul to bootstrap columns:
<div class="container">
<div class="row">
<?php foreach ( $myposts as $post ) : setup_postdata($post ); ?>
<div class="col-sm-4">
<?php the_title(); ?>
</div>
<?php endforeach; wp_reset_postdata(); ?>
</div>
</div>
If you want to use custom layout then you need to make a custom template and there you will add a page for using your custom templet. your custom template code will like this
<?php
/* Template Name: Your custom templete */
get_header();
?><?php $the_query = new WP_Query(array(
'category_name' => 'popular',
'posts_per_page' => '6',
'order' => 'DESC', // Show only the published posts
));?>
<?php if( $the_query->have_posts() ): ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post();?>
<div class="story-info">
<a class="category-name arts texunset" href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<span class="daycolor" style="background:<?php the_field('colorpost'); ?>;"> </span>
<span>
<?php the_title(); ?>
</span>
</a>
<div class="date">
<?php the_time('F jS, Y') ?> |
<i class="fa fa-signal"></i>
</div>
</div>
<hr>
<?php endwhile; ?>
<?php endif; ?>
<?php get_footer();?>

Wordpress - How do I display posts grouped by custom taxonomy?

I am creating a FAQ page using a custom post type and custom taxonomy. I'm trying to create an unordered list for each taxonomy in order to group the FAQ's. In that unordered list, I want the first listed item to be the taxonomy name and then repeat the second listed item for all the questions in the taxonomy. This is the page I'm working on link.
It's currently duplicating the posts instead of displaying in the rightful taxonomies.
<?php
// get all the categories from the database
$cats = get_terms( array(
'taxonomy' => 'faq_categories',
));
// loop through the categories
foreach ($cats as $cat) {
// setup the category ID
$cat_id = $cat->term_id;
?>
<!-- Make a header for the category -->
<ul id="<?php echo $cat->slug; ?>" class="cd-faq-group">
<li class="cd-faq-title">
<h2>Questions <?php echo $cat->name; ?></h2>
</li>
<?php
// create a custom wordpress query
query_posts( array(
'post_type' => 'faqs',
'tax_query' => array(
array(
'taxonomy' => 'faq_categories', //or tag or custom taxonomy
'field' => 'slug',
'terms' => 'for-women'
)
)
));
// start the wordpress loop!
if (have_posts()) : while (have_posts()) : the_post(); ?>
<li>
<a class="cd-faq-trigger" href="#0"><?php the_title(); ?></a>
<div class="cd-faq-content">
<?php the_content(); ?>
</div>
</li>
<?php endwhile; endif; // done our wordpress loop. Will start again for each category
wp_reset_postdata();
?>
</ul>
<?php } // done the foreach statement ?>
Your query is not changing as you iterate through the $cats array. Perhaps changing the the value of the 'terms' array to $cat->slug would give you better results.
Thank you so much. You have both provided great insight as to what I was missing. I've resolved it now and this is how I went about solving it taking your suggestions into consideration.
<?php
$cats = get_terms(
array(
'taxonomy' => 'faq_categories',
'orderby' => 'term_id',
'order' => 'ASC'
)
);
foreach ($cats as $cat) :
?>
<ul id="<?php echo $cat->slug; ?>" class="cd-faq-group">
<li class="cd-faq-title">
<h2>Questions <?php echo $cat->name; ?></h2>
</li>
<?php
$questions = new WP_Query(
array(
'category_name' => $cat->slug
)
);
$questions = new WP_Query( array(
'post_type' => 'faqs',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'faq_categories',
'field' => 'slug',
'terms' => array($cat->slug),
)
)
));
?>
<?php if ($questions->have_posts()) : while ($questions->have_posts()) : $questions->the_post();?>
<li>
<a class="cd-faq-trigger" href="#0"><?php the_title(); ?></a>
<div class="cd-faq-content">
<?php the_content(); ?>
</div>
</li>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
In your query_post, your tax_query's field should be term_id and your terms be assigned to your $cat_id variable, instead of a hardcoded term.

my taxonomy-$taxonomy.php page post not show?

I created a custom post taxonomy.Now i want to show all specific post by specific taxonomy. so I created a taxonomy-product_cat.php page.
here is product page get term and link code--
<div class="side_box side_box_1 red5">
<h5>Filter Products</h5>
<h6>Brand</h6>
<?php $topics = get_terms('product_cat');
echo '<ul class="advanced-filters tgl_c">';
foreach ($topics as $topic) {
echo '<li class="advanced-filter" data-group="Brand">'.$topic->name.'</li>';
}
echo '</ul>';?>
</div>
here is custom post taxonomy code---
function product_taxonomy() {
register_taxonomy(
'product_cat', //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
'product', //post type name
array(
'hierarchical' => true,
'label' => 'product Category', //Display name
'query_var' => true,
'show_admin_column' => true,
'rewrite' => array(
'slug' => 'product-category', // This controls the base slug that will display before each term
'with_front' => false // Don't display the category base before
)
)
);
}
add_action( 'init', 'product_taxonomy');
And here is taxonomy-product_cat.php page code--
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); $unique = "_product_"; ?>
<div class="col-md-3 col-xs-6 element mb30">
<div class="main_box">
<div class="box_1">
<div class="product-image">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( $post->ID, 'product-image',array('class' => 'img-responsive') );?>
</a>
</div>
<div class="overlay hidden-sm hidden-xs">
More Info
</div>
</div>
<div class="desc">
<h5><?php the_title(); ?></h5>
<p><?php echo get_post_meta(get_the_ID(),$unique.'brand_name',true); ?></p>
</div>
</div>
</div>
<?php endwhile; ?>
<?php else :?>
<h3><?php _e( 'Not Found Any Product.' ); ?></h3>
<?php endif ?>
But the result is Not Found Any Product.So please someone help me how can i fix this problem.Thanks
see documentation
https://developer.wordpress.org/reference/functions/get_terms/
specifically
Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args array:
$terms = get_terms( array(
'taxonomy' => 'post_tag',
'hide_empty' => false,
) );
instead of the first parameter being the taxonomies.
get_terms('product_cat');
You should do
get_terms(array(
'taxonomy' => 'product_cat'
));
Not sure if that is the issue, but seems the most obvious thing.
UPDATE
Did you try this
get_terms(array(
'taxonomy' => 'product_cat',
'hide_empty' => 0
));
This will show the taxonomy if you didn't insert any actual terms with
wp_insert_term()
As per this page,
https://wordpress.org/support/topic/get_terms-does-not-return-my-custom-taxonomy-terms
I don't typically work with WP on this level, but my google skills are unmatched ... lol

WordPress get by and exclude by meta data messes pagination

So this is my first post and wow, I didn't know this site existed. I've had a look around at questions and I hope that mine isnt a dumb nooby one. Although I am a noob :S
Ok, so I created a function in WordPress that will add a meta box to the new posts page so that I can specify whether or not this post should be featured (I read this is better than creating a featured category for SEO purposes?).
Anyway.. The code I have works in showing the most recent. Here is the code for that:
<?php
$args=array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$custom = get_post_meta($my_query->post->ID, '_featuredpost_meta_value_key', true);
if ( $custom ){
?>
<article class="container" itemprop="blogPosts" itemscope itemtype="http://schema.org/BlogPosting">
<div class="row">
<h2 itemprop="about">
<?php the_title(); ?>
</h2>
</div>
<div class="row">
<div class="<?php if ( has_post_thumbnail() ) { ?>two-thirds column<?php } else {?> twelve columns <?php } ?>">
<p class="post-excerpt"><?php modified_excerpt(); ?></p>
</div>
<?php if ( has_post_thumbnail() ) { ?>
<div class="one-third column">
<?php the_post_thumbnail('full', array('class'=>'hide-mobile')); ?>
</div>
<?php } ?>
</div>
<div class="row">
Continue Reading
</div>
<hr />
<div class="post-info">
<ul>
<li class="date"><?php the_date();?></li>
<li class="author"><?php echo get_the_author_meta('display_name'); ?></li>
<li class="category"><?php the_category(', '); ?></li>
<li class="tags"><?php the_tags('',', ',''); ?></li>
</ul>
</div>
</article>
<?php
}
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
Now, When I use the same code below, but then use:
if ( ! $custom ){
to show the posts that are not set to be featured that also works. The problem is that the pagination no longer works. When I go to the second page it just duplicates what is on the home page.
This leads me to believe that I have created a mashed together crappy bit of code. Can someone please help me build a loop, that will exclude any posts where the meta data _featuredpost_meta_value_key is set to Yes.
Thanks in advance
You'll want to use a WP Meta Query in your original $args array.
https://codex.wordpress.org/Class_Reference/WP_Meta_Query
From the docs, here's an example:
$meta_query_args = array(
'relation' => 'OR', // Optional, defaults to "AND"
array(
'key' => '_my_custom_key',
'value' => 'Value I am looking for',
'compare' => '='
)
);
$meta_query = new WP_Meta_Query( $meta_query_args );
But you can also use the sugar provided by the WP_Query class and pass it in as the meta_query value to your original args:
$args=array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1,
'meta_query' => array(
'key' => '_featuredpost_meta_value_key',
'value' => 'Yes',
'compare' => '='
)
);

display content using get posts from terms under a custom taxonomy

I am having a custom taxonomy say "project-type" which is registered for the custom post "projects" and under that I have terms "catOne" and "catTwo". Now I want to display all the custom posts that are linked to catOne using the term_id of "catOne", which in my case is 9.
So I am successfully able to loop through all the posts but it is displaying only the ID but not all contents.
My approach:
$cat_id = 9;
$args = array(
'post_type' => 'projects',
'tax_query' => array(
array(
'taxonomy' => 'project-type',
'field' => 'term_id',
'terms' => array( $cat_id )
),
),
);
$posts = get_posts( $args );
foreach ( $posts as $post ) {
setup_postdata( $post ); ?>
<div id="post-<?php echo $post->ID; ?> <?php post_class(); ?>">
<h1 class="posttitle"><?php the_title(); ?></h1>
<div id="post-content">
<?php the_excerpt(); ?>
</div>
</div>
<?php } wp_reset_postdata();
Output I am getting
<div id="post-137 class=" ""="">
<h1 class="posttitle"></h1>
<div id="post-content">
</div>
</div>
<div id="post-135 class=" ""="">
<h1 class="posttitle"></h1>
<div id="post-content">
</div>
</div>
Can someone please help me with where I am going wrong?
Instead of using post_class(), the_permalink(), the_title(), and the_excerpt(), you should use the $post object to get the data, just like you did with $post->ID. The functions you've used should be called only if you're using a loop based on have_posts(). You aren't, so replace them with:
post_class() -> get_post_class( '', $post->ID )
the_permalink() -> get_the_permalink( $post->ID )
the_title() -> $post->title
the_excerpt() -> $post->post_excerpt

Categories