Open wordpress post in a different page - php

I have two separate pages showing a list of different categories linking to the full post in wordpress. These both currently open on single.php in my theme but The categories need to be styled differently on each page.
I have created the template page but do not know how to open posts on another page other than single.php
to simplify: how do I open posts on another version of single.php
the code I have that opens the full posts are:
<?php // PAGE LINK/TITLE
if (is_page()) {
$cat=get_cat_ID($post->post_title); //use page title to get a category ID
$posts = get_posts ("category_name=case-study&posts_per_page=10");
if ($posts) {
foreach ($posts as $post):
setup_postdata($post);
?>
<div class="serve-inner-split">
<div id="case-split">
<div id="case-left" class=" serve-left">
<div id="case-study-content">
<h1 class="case-study-title"><?php the_title(); ?></h1>
<p><?php //PULLS IN EXCERPT
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>
</p>
READ CASE STUDY
</div>
</div>
<div id="case-right" class="serve-grey">
<?php
if ( has_post_thumbnail() ) { // PULLS IN IMAGE check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
</div>
</div>
</div>
<?php endforeach;
}
}
?>

Hello :) I would achieve this by creating custom post type for each case you need to display the single post differently. You may refer to WP Codex https://codex.wordpress.org/Post_Types on this case. Eventually it will allow you to create as much single post templates as you want.

I'd create extra category fields. In functions.php add
add_action ( 'edit_category_form_fields', 'mytheme_extra_category_fields');
add_action ( 'category_add_form_fields', 'mytheme_extra_add_category_fields');
if ( ! function_exists( 'mytheme_extra_category_fields' ) ){
function mytheme_extra_category_fields( $tag ) {
$t_id = $tag->term_id;
$cat_meta = get_option( "category_$t_id");
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="extra1"><?php esc_attr_e('Blog Layout', 'mytheme'); ?></label></th>
<td>
<select name="Cat_meta[blog_layout]">
<?php
echo '<option value="layout1" '.selected( $cat_meta['blog_layout'], 'layout1', false).'>'.esc_html__('Layout 1', 'mytheme').'</option> ';
echo '<option value="layout2" '.selected( $cat_meta['blog_layout'], 'layout2', false).'>'.esc_html__('Layout 2', 'mytheme').'</option> ';
?>
</select>
</td>
</tr>
<?php
}
}
if ( ! function_exists( 'mytheme_extra_add_category_fields' ) ){
function mytheme_extra_add_category_fields( $tag ) {
$t_id = (is_object($tag))?$tag->term_id:'';
$cat_meta = get_option( "category_$t_id");
?>
<div class="form-field">
<label for="extra1"><?php esc_attr_e('Blog Layout', 'mytheme'); ?></label></th>
<select name="Cat_meta[blog_layout]">
<?php
echo '<option value="layout1" '.selected( $cat_meta['blog_layout'], 'layout1', false).'>'.esc_html__('Layout 1', 'mytheme').'</option> ';
echo '<option value="layout2" '.selected( $cat_meta['blog_layout'], 'layout2', false).'>'.esc_html__('Layout 2', 'mytheme').'</option> ';
?>
</select>
</div>
<?php
}
}
add_action ( 'edited_category', 'mytheme_save_extra_category_fileds');
add_action ( 'created_category', 'mytheme_save_extra_category_fileds');
if ( ! function_exists( 'mytheme_save_extra_category_fileds' ) ){
function mytheme_save_extra_category_fileds( $term_id ) {
if ( isset( $_POST['Cat_meta'] ) ) {
$t_id = $term_id;
$cat_meta = get_option( "category_$t_id");
$cat_keys = array_keys($_POST['Cat_meta']);
foreach ($cat_keys as $key){
if(isset($_POST['Cat_meta'][$key])){
$cat_meta[$key] = $_POST['Cat_meta'][$key];
}
}
update_option( "category_$t_id", $cat_meta );
}
}
}
This should output the select 'Blog layout' when you add categories to your posts. Then in your index.php add
$cat_id = get_query_var('cat');
$cat_data = get_option("category_$cat_id");
<?php if(isset($cat_data['blog_layout']) && $cat_data['blog_layout'] == 'layout1'): ?>
\\Your layout1 here
<?php else: ?>
\\Your layout2 here
<?php endif; ?>
This should work.

To fix this issue I replaces the entire single.php file with this code:
<?php
if (in_category('2')) {include (TEMPLATEPATH . '/page-case-study-post.php');
}
else { include (TEMPLATEPATH . '/page-services-post.php');
}
?>;
sending either categories to two different pages depending on what its set to.

Related

Adding ACF to Woocommerce ul.products on Shop Page

I'm using WooCommerce and Advanced Custom Fields, where ACF group is set up with post type for products. I would like to add a couple of simple text fields to the products box below the product title, and it will display on all products.
I have looked and found the hook for this to be woocommerce_after_shop_loop_item_title
Image attached for visual description.
Like this I'm looking to add Address value ($location), Bedrooms value ($bed) and Bathrooms ($bath).
Please understand I am very new to PHP and still learning. I have tried to make an attempt, however I am unsure how to extract the field data from the product's post.
Any tips in the right direction to learn would really be much appreciated.
Thank you in advance.
add_action( 'woocommerce_after_shop_loop_item_title', 'woo_products_property', 1 );
function woo_products_property() {
?>
<div class="property_loop_bottom_sec">
<?php $location = get_field_object('address'); ?>
<?php if( ! empty( $location ) ) { ?>
<div class="feature">
<div class="value"><i class="et-pb-icon map-marker"></i><?php echo $location['value'];?></div>
</div>
<?php } ?>
<?php $bed = get_field_object('bedroom'); ?>
<?php if( ! empty( $bed ) ) { ?>
<div class="feature">
<div class="value"><i class="fas fa-bed"></i><?php echo $bed['value'];?></div>
<span>Bed</span>
</div>
<?php } ?>
<?php $bath = get_field_object('bathroom'); ?>
<?php if( ! empty( $bath ) ) { ?>
<div class="feature">
<div class="value"><i class="fas fa-bath"></i><?php echo $bath['value'];?></div>
<span>Bath</span>
</div>
<?php } ?>
</div>
<?php
} ?>
I revised your code. Try the below code.
function woo_products_property() {
global $post;
?>
<div class="property_loop_bottom_sec">
<?php
$location = get_field( 'address', $post->ID );
if( ! empty( $location ) ) { ?>
<div class="feature">
<div class="value"><i class="et-pb-icon map-marker"></i><?php echo $location['value'];?></div>
</div>
<?php }
$bed = get_field( 'bedroom', $post->ID );
if( ! empty( $bed ) ) { ?>
<div class="feature">
<div class="value"><i class="fas fa-bed"></i><?php echo $bed['value'];?></div>
<span>Bed</span>
</div>
<?php }
$bath = get_field( 'bathroom', $post->ID);
if( ! empty( $bath ) ) { ?>
<div class="feature">
<div class="value"><i class="fas fa-bath"></i><?php echo $bath['value'];?></div>
<span>Bath</span>
</div>
<?php } ?>
</div>
<?php }
add_action( 'woocommerce_after_shop_loop_item_title', 'woo_products_property', 1 );
Tested and works

Insert <div> under the header on a specific category page

In my Wordpress website i made this code in header.php that write html under the header only if you are on a specific post (product) page,
<?php
$post = get_post();
if ( $post->ID == 'postid' ){
echo '<div>
<p>'text'
</p>
</div>
</div>
</div>';
}
?>
I would like to make the same thing in a specific category page doing something like this
<?php
$postcat = get_the_category();
if ( $postcat->ID == 'categoryid'){{
echo '<div>
<p>'text'
</p>
</div>
</div>
</div>';
}
?>
But it doesn't work, i also tried different method like
<?php if (is_category('categoryname')) : ?>
<div></div>
?php endif;?>
How can i do this?
Thanks.
You can use is_product_category. check the below code.
<?php
if ( is_product_category( 'your-category-slug' ) ){
echo '<div>
<p>'text'</p>
</div>
</div>
</div>';
}
?>
USEFUL LINKE
conditional-tags
If post = specific ID
<?php
$post = get_post();
if ( $post->ID === 'postid' ){
echo "<div>";
echo $post->post_content;
echo "</div>";
}
?>
Is post has category "supercat"
<?php
$post = get_post();
if ( has_category('supercat', $post->ID)){
echo "<div>";
echo $post->post_content;
echo "</div>";
}
?>
For products , check Bhautik's answer

How to display a specific category in wordpress post by id or other

I am trying to create a related post section at the bottom of each post. I am using YARPP plugin to generate the related posts. I want customise the a template to show, a thumbnail(Done), title(Done) and a specific category(Issue).
My issue is its displaying all assigned categories, I only want to display one child category. I have a speaker category with child categories of the names of the speakers.
Let me know if you need more information. Thanks
<h3>Related Posts</h3>
<?php if ( have_posts() ) : ?>
<div>
<?php
while ( have_posts() ) : the_post(); ?>
<div>
<?php if ( has_post_thumbnail() ) : ?>
<?php the_post_thumbnail( $dimensions['size'], array( 'data-pin-nopin' => 'true' ) ); ?></a>
<a href="<?php the_permalink(); ?>" rel="bookmark norewrite" title="<?php the_title_attribute(); ?>">
</div>
<div>
<?php the_title(); ?>
<!-- How to add specific category -->
<?php
$categories = get_the_category();
$separator = ' ';
$output = '';
if ( ! empty( $categories ) ) {
foreach( $categories as $category ) {
$term_id = '328';
$output .= '|| ' . esc_html( $category->name ) . '' . $separator;
}
echo trim( $output, $separator );
}
?>
<!-- How to add specific category END -->
</div>
<?php endif; ?>
<?php endwhile; ?>
</div>
<?php else : ?>
<p>No related photos.</p>
<?php endif; ?>
<?php $related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 6, 'post__not_in' => array($post->ID) ) );
if( $related ) foreach( $related as $post ) { setup_postdata($post); ?>
<div class="post">
<div class="post-title">
<h6 class="title"><?php the_title() ?></h6>
</div>
<div class="post-img" style="background-image: url(<?php the_post_thumbnail_url(); ?>)"></div>
</div>
<?php } wp_reset_postdata(); ?>

If statement stops working after 9 items

Within woo commerce I am pulling all of the featured items into the home page. Within the featured Items I have an if statement nested so that it checks to see what category each featured item is in.
I am doing so so that if its in the category flower I can ad the suffix OZ to the price and within concentrates I can add the suffix GM to the price.
It works great with 9 items or less. Once more than 9 items are applied they are all displaying OZ no matter what the category.
There are a few other things in there checking for logged in vs not logged in. but here is the whole snippet
<!------ check cat -->
<?php
global $post;
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $categories[] = $term->slug;
?>
<?php if ( in_array( 'flower', $categories ) ) { ?>
<div class="price_home">
<div class="product__inside__price price-box">
<?php if ( $price_html = $product->get_price_html() ) : ?>
<span class="price"><?php echo wp_kses_post($price_html); ?> oz</span>
<?php endif; ?>
</div>
</div>
<?php } elseif ( in_array( 'concentrates', $categories ) ) { ?>
<div class="price_home">
<div class="product__inside__price price-box">
<?php if ( $price_html = $product->get_price_html() ) : ?>
<span class="price"><?php echo wp_kses_post($price_html); ?> gm</span>
<?php endif; ?>
</div>
</div>
<?php } else { ?>
<div class="price_home">
<div class="product__inside__price price-box">
<?php if ( $price_html = $product->get_price_html() ) : ?>
<span class="price"><?php echo wp_kses_post($price_html); ?
</span>
<?php endif; ?>
</div>
</div>
<? } ?>
I simplified this code but as I stated in the comments. This is used to split out the featured products. So I'm not 100% sure this is the issue, but people were having an issue viewing so much code.

I can't get the value of option in select with settings api wordpress

Hello I try to make selectbox with settings api on Wordpress. I think everything is working awesome except $options variable. This variable doesn't get settings fields value. But $options have to get value after saving. What is the problem?
Register and settings field:
add_settings_field (
'slideral', // ID used to identify the field throughout the theme
'Slider:', // The label to the left of the option interface element
'slider_callback', // The name of the function responsible for rendering the option interface
'slider_dizi', // The page on which this option will be displayed
'page_1_section', // The name of the section to which this field belongs
array( // The array of arguments to pass to the callback. In this case, just a description.
'This is the description of the option 1',
)
);
register_setting(
'setting-group-1',
'slideral'
);
Callback:
function slider_callback() {
$categories = get_categories();
$options = esc_attr( get_option( 'slideral' ) );
?>
<select id="slideral_select" name="slider_dizi">
<?php
foreach ($categories as $category ) { ?>
<option value="<?php echo $category->name; ?>" <?php if( $options == $category->name) { echo 'selected="selected"'; }?>><?php echo $category->name; ?></option>
<?php }
?>
</select>
<p class="description"> Slider'da görülmesini istediğiniz kategoriyi seçiniz </p>
<?php
}
--------------------------- EDİT ---------------------------------------
add_settings_section(
'page_1_section', // ID used to identify this section and with which to register options
'Header Ayarları', // Title to be displayed on the administration page
'header_section_callback', // Callback used to render the description of the section
'slider_dizi' // Page on which to add this section of options
);
AND TABS :
function my_menu_page() {
?>
<?php
if( isset( $_GET[ 'tab' ] ) ) {
$active_tab = $_GET[ 'tab' ];
} else {
$active_tab = 'anasayfa';
}
?>
<div class="wrap">
<h2> Tema Ayarları </h2>
<div class="description"> Temanızın ayarlarını bu sayfa içerisinde düzenleyebilirsiniz </div>
<?php settings_errors(); ?>
<h2 class="nav-tab-wrapper">
Anasayfa
İletişim
</h2>
<form method="post" action="options.php">
<?php
if( $active_tab == 'anasayfa' ) {
settings_fields( 'setting-group-1' );
do_settings_sections( 'slider_dizi' );
} else if( $active_tab == 'iletisim' ) {
settings_fields( 'setting-group-2' );
do_settings_sections( 'my-menu-slug-2' );
}
?>
<?php submit_button(); ?>
</form>
</div>
<?php
}

Categories